package Servidor.Modelo.Games;

import java.util.Timer;
import java.util.TimerTask;
import java.util.ArrayList;



/**
 * @author Angel Lopez
 */
public class GameRunning {
    
    private Game data_game;
    private BoardPlaying board;
    private ArrayList<Player> list_player;
    private Player player_aux;
    private int round;
    private int player_turn;
    private int position;
    private int roll_dice1;
    private int roll_dice2;
    private int player_in_pit;
    private String message;
    private boolean pass;
    private Timer timer;
    
    public GameRunning(Game data_game){
        this.data_game = data_game;
        round=0;
        player_turn=0;
        player_in_pit = -1;
        list_player = data_game.get_list_payers();
        roll_dice1=0;
        roll_dice2=0;
        position = 0;
        message = message + "";
        pass = true;
        
        board = data_game.getBoardPlaying();

    }
    
    public void roll_dice(int dice1, int dice2){
        roll_dice1 = dice1;
        roll_dice2 = dice2;
    }
    
    public void iniciar_juego(){
        IniciarTarea task = new IniciarTarea();    
        timer = new Timer();
        timer.schedule(task, 0, 1000);
    }

    
    private class IniciarTarea extends TimerTask{

        
        @Override
        public void run() {
            position = 0;
            pass = true;
            
            //if there arent active player playing the game finish
            if(data_game.number_player_active()==0){
                message = ("GAME FINISHED, THERE ARE NOT PLAYER ATIVE");
                update_board();
                data_game.finish_game();
                timer.cancel();
                
            }
            
            player_aux = list_player.get(player_turn);
                
            message = "";

            if(player_aux.getwait()!=0){
                player_aux.setwait(player_aux.getwait()-1);
            }
            
            
          
            
            //PASAR TURNO
            if(!player_aux.isActive()){
               position = 0;
               message = ("PLAYER("+ (player_turn+1) +") - IS NOT ACTIVE");
               update_board();
               pass_turn();
            }
            else if(player_aux.getwait()!=0){
               position = board.get_position_player(player_turn+1);
               message = ("PLAYER("+ (player_turn+1) +") - SQUARE(" + position + ") - WAITING, TURN LEFT " + player_aux.getwait() );
               
               update_board();

               pass_turn();
               
            } //POZO
            else if(player_aux.isPit()){
               position = board.get_position_player(player_turn+1);
               message = ("PLAYER("+ (player_turn+1) +") - SQUARE(" + position + ") - LOST ONE TURN IN PIT ");
               
               if(data_game.number_player_active()==1){
                    message = ("GAME FINISHED, YOU LOSE!");
                    update_board();
                    data_game.finish_game();
                    timer.cancel();
                }
               
               
               if(player_aux.getwait()==0)
                   player_aux.setwait(player_aux.getwait()+1);
               
               
               update_board();
               pass_turn();
            }
            else{
            
                if(((roll_dice1+roll_dice2)!=0)&&(player_aux.getwait()==0)){
 
                    
                    
                    position = board.get_position_player(player_turn+1);
                    if(position>=60)
                        position = position + roll_dice1;
                    else
                        position = position + (roll_dice1+roll_dice2);
                    
                    if(position>63){
                        int back = (position-63);
                        position = 63 - back;
                        message = "PLAYER("+ (player_turn+1) +") - SQUARE(" + position + ") - MOVE BACK " +(back) + " SQUARES" ;
                    }   
                    else{
                       message = "PLAYER("+ (player_turn+1) +") - SQUARE(" + position + ")"; 
                    }
                        
                    
                    //TODO AQUI EMPIEZAN LAS COMPROBACIONES
                    
                    switch (position){
                        case 5:{
                            message = message + " - OCA! GO(9) AND ROLL AGAIN";
                            position = 9;
                            pass = false;
                            break;
                        }
                        case 6:{
                            message = message + " - BRIDGE! GO(19) AND LOSE ONE TURN IN INN";
                            position = 19;
                            //to lost one turn
                            player_aux.setwait(player_aux.getwait()+2);
                            break;
                        }
                        case 9:{
                            message = message + " - OCA! GO(14) AND ROLL AGAIN";
                            position = 14;
                            pass = false;
                            break;
                        }
                        case 12:{
                            message = message + " - BRIDGE! GO(19) AND LOSE ONE TURN IN INN";
                            position = 19;
                            //to lost one turn
                            player_aux.setwait(player_aux.getwait()+2);
                            break;
                        }
                        case 14:{
                            message = message + " - OCA! GO(18) AND ROLL AGAIN";
                            position = 18;
                            pass = false;
                            break;
                        }
                        case 18:{
                            message = message + " - OCA! GO(23) AND ROLL AGAIN";
                            position = 23;
                            pass = false;
                            break;
                        }
                        
                        case 19:{
                            message = message + " - INN! LOSE ONE TURN IN INN";
                            //to lost one turn
                            player_aux.setwait(player_aux.getwait()+2);
                            break;
                        }
                        
                        case 23:{
                            message = message + " - OCA! GO(27) AND ROLL AGAIN";
                            position = 27;
                            pass = false;
                            break;
                        }
                        case 26:{
                            message = message + " - DICES! ROLL AGAIN! ";
                            pass = false;
                            break;
                        }
                        case 27:{
                            message = message + " - OCA! GO(32) AND ROLL AGAIN";
                            position = 32;
                            pass = false;
                            break;
                        }                            
                        
                        case 31:{
                            message = message + " - PIT! WAIT ANOTHER PLAYER FALL IN PIT";
                            
                            player_aux.setPit(true);
                            
                            if(player_in_pit!=-1)
                                list_player.get(player_in_pit).setPit(false);
                                
                            player_in_pit = player_turn;   
                            break;
                        }

                        case 32:{
                            message = message + " - OCA! GO(36) AND ROLL AGAIN";
                            position = 36;
                            pass = false;
                            break;
                        }
                            
                        case 36:{
                            message = message + " - OCA! GO(36) AND ROLL AGAIN";
                            position = 41;
                            pass = false;
                            break;
                        }
                            
                        case 41:{
                            message = message + " -  OCA! GO(45) AND ROLL AGAIN";
                            position = 45;
                            pass = false;
                            break;
                        }                               

                        case 42:{
                            message = message + " - LABYRINTH OF CHINOTAURO! GO(30)";
                            position = 30;
                            break;
                        } 

                        case 45:{
                            message = message + " - OCA! GO(50) AND ROLL AGAIN";
                            position = 50;
                            pass = false;
                            break;
                        }  
                            
                        case 50:{
                            message = message + " - OCA! GO(54) AND ROLL AGAIN";
                            position = 54;
                            pass = false;
                            break;
                        }

                        case 53:{
                            message = message + " - DICES! ROLL AGAIN! ";
                            pass = false;
                            break;
                        }
                            
                            
                        case 54:{
                            message = message + " - OCA! GO(59) AND ROLL AGAIN";
                            position = 59;
                            pass = false;
                            break;
                        }
                            
                        case 56:{
                            message = message + " - JAIL! LOSE THREE TURNS IN JAIL";
                            //lost one turn
                            player_aux.setwait(player_aux.getwait()+4);
                            break;
                        }
                            
                        case 58:{
                            message = message + " - DEATH! START TO BEGGINING THE GAME";
                            position = 1;
                            break;
                        }
                            
                        case 59:{
                            message = message + " - OCA! GO(63)";
                            position = 63;
                            break;
                        }
                            
                        case 63:{
                            message = message + " -  YOU WIN!";
                            data_game.setId_winner(player_aux.getId_player());
                            data_game.finish_game();
                            timer.cancel();
                            break;
                        }
     
                    }
                    
                    update_board();

                    if(player_aux.getwait()==0)
                        player_aux.setwait(player_aux.getwait()+1);
                    if(pass)
                        pass_turn();
                }
            }
            
            
            roll_dice1 = 0;
            roll_dice2 = 0;
            

        }
    }
    

    
    public void update_board(){
        board.modificate_board(round, (player_turn+1), position, roll_dice1, roll_dice2, message);
        data_game.notify_change(board);
    }
    
    public void pass_turn(){
        player_turn++;

        if(player_turn >= data_game.number_player()){
            player_turn =0;
            round++;
            
            
        }
    }
    
    public boolean list_player_is_empty(){
        return list_player.isEmpty();
    }
    
    
    
    
    
}